package com.xuewei.pagedemo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xuewei.pagedemo.entity.TUser;
import com.xuewei.pagedemo.mapper.TUserMapper;
import com.xuewei.pagedemo.service.ITUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xuewei.pagedemo.util.BeansUtil;
import com.xuewei.pagedemo.util.PagerResult;
import com.xuewei.pagedemo.vo.PageVo;
import com.xuewei.pagedemo.vo.TUserVo;
import com.xuewei.springcloudcommon.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zhangxuewei
 * @since 2023-04-10
 */
@Service
public class TUserServiceImpl extends ServiceImpl<TUserMapper, TUser> implements ITUserService {

    @Autowired
    TUserMapper tUserMapper;

    /**
     * PageHelper传统使用方式
     * 注意： PageHelper.startPage(pageVo.getCurrent(), pageVo.getSize());  仅对下面第一个数据库查询起作用
     * @param pageVo
     * @return
     */
    @Override
    public Result page1(PageVo pageVo) {
        //设置分页
        PageHelper.startPage(pageVo.getCurrent(), pageVo.getSize());
        //查询列表
        List<TUser> list = this.list();
        //对象复制
        List<TUserVo> tUserVos = BeansUtil.convert(list, TUserVo.class);

        PageInfo pageInfo = new PageInfo(tUserVos);
        return Result.ok(pageInfo);
    }

    /**
     * PageHelper重写返回值，简洁
     * @param pageVo
     * @return
     */
    @Override
    public Result page2(PageVo pageVo) {
        Page page = PageHelper.startPage(pageVo.getCurrent(), pageVo.getSize());
        //查询列表
        List<TUser> list = this.list();
        //对象复制
        List<TUserVo> tUserVos = BeansUtil.convert(list, TUserVo.class);
        return Result.ok(new PagerResult<TUserVo>(page.getTotal(), tUserVos));
    }

    /**
     * IPage基础试用
     * 不支持自定义SQL
     * 支持带条件查询
     * 返回值实体类为数据库实体类
     * @param pageVo
     * @return
     */
    @Override
    public Result page3(PageVo pageVo) {
        //设置分页page
        IPage<TUser> page = new com.baomidou.mybatisplus.extension.plugins.pagination.Page(pageVo.getCurrent(), pageVo.getSize());
        //查询条件
        QueryWrapper<TUser> wrapper = new QueryWrapper();
        //分页
        IPage<TUser> iPage = this.page(page, wrapper);
        //数据处理
        List<TUser> records = iPage.getRecords();

        iPage.setRecords(records);
        return Result.ok(iPage);
    }

    /**
     * IPage灵活应用
     * 支持自定义SQL
     * 返回值实体类可自定义
     * @param pageVo
     * @return
     */
    @Override
    public Result page4(PageVo pageVo) {
        com.baomidou.mybatisplus.extension.plugins.pagination.Page<Map<String, Object>> page =
                new com.baomidou.mybatisplus.extension.plugins.pagination.Page<>(pageVo.getCurrent(), pageVo.getSize());
        IPage<TUserVo> tUserVoIPage = this.baseMapper.queryPageByDept(page, pageVo.getDeptName());
        return Result.ok(tUserVoIPage);

    }

    @Override
    public void add() {
        TUser user = new TUser();
        user.setName("周瑜");
        user.setAge(20);
        user.setDeptId(200);
        this.save(user);
    }


}
